package com.game.framework.orm.mongodb;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoTypeMapper;
/**
 * 调用mongoTemplate的save方法时, 
 * spring-data-mongodb的TypeConverter会自动给document添加一个_class属性, 
 * 值是你保存的类名. 这种设计并没有什么坏处. 
 * spring-data-mongodb是为了在把document转换成Java对象时能够转换到具体的子类.
 * 但有时候我们并不希望出现这个字段, 主要是看上去会比较"烦". 
 * 可以通过设置MappingMongoConverter的MongoTypeMapper来解决这个问题.
 * 把spring-data的MappingMongoConverter拿过来,然后把TypeMapper设置一下.
 * @author Quake
 *
 */
public class MongoConverterEnhanceFactoryBean implements FactoryBean<MappingMongoConverter> {
	
	private MappingMongoConverter converter;

	public void setConverter(MappingMongoConverter converter) {
		this.converter = converter;
	}
	
	@Override
	public MappingMongoConverter getObject() throws Exception {
		MongoTypeMapper typeMapper = new DefaultMongoTypeMapper(null);
		converter.setTypeMapper(typeMapper);
		return converter;
	}

	@Override
	public Class<?> getObjectType() {
		return MappingMongoConverter.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}
